#pragma once

#include <iostream>
#include <stdarg.h>
#include <time.h>

using namespace std;

#define SIZE 1024

#define INFO 0
#define DEBUG 1
#define WARNING 2
#define ERROR 3
#define FATAL 4

#define Screen 1
#define Onefile 2
#define ClassFile 3

#define LogFile "log.txt"

class Log
{
public:
    Log()
    {
        _printMethod = 1;
    }
    void Enable(int method)
    {
        _printMethod = method;
    }
    string levelToString(int level)
    {
        switch (level)
        {
        case INFO:
            return "INFO";
        case DEBUG:
            return "DEBUG";
        case WARNING:
            return "WARNING";
        case ERROR:
            return "ERROR";
        case FATAL:
            return "FATAL";
        default:
            return "NONE";
        }
    }

    void logmessage(int level, const char *format, ...)
    {
        // cout << time(nullptr) << endl;

        time_t t = time(nullptr);
        struct tm *ctime = localtime(&t);

        char leftbuffer[SIZE];
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(),
                 ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

        va_list s;
        va_start(s, format);
        char rightbuffer[SIZE];
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
        va_end(s);

        char logtxt[SIZE * 2];
        snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftbuffer, rightbuffer);

        // printf("%s", logtxt);
        printlog(level, logtxt);
        // printf("%d-%d-%d %d:%d:%d\n", ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday,\
    //         ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

        // 格式：默认部分 + 自定义部分（可变参数部分）
    }

    void printlog(int level, const string& logtxt)
    {
        switch (_printMethod)
        {
        case Screen:
            cout << logtxt << endl;
            break;
        case Onefile:
            printOneFile(logtxt);
            break;
        case ClassFile:
            printClassFile(level, logtxt);
            break;
        default:
            break;
        }
    }

    void printOneFile(const string& logtxt)
    {
        int fd = open(LogFile, O_WRONLY|O_CREAT|O_APPEND, 0666);  // "log.txt"
        if(fd < 0)
        {
            return;
        }
        write(fd, logtxt.c_str(), logtxt.size());
        close(fd);
    }

    void printClassFile(int level, const string& logtxt)   // "log.txt.debug/info"
    {
        string filename = LogFile;
        // filename += logStuffix(level);
        filename += ".";
        filename += levelToString(level);

       
        write(fd, logtxt.c_str(), logtxt.size());
    }
    ~Log()
    {
    }

private:
    int _printMethod;
};

// 可变参数必须至少有一个具体的参数
int sum(int n, ...)
{
    // va_list是一个char*类型
    // vs_list是宏
    va_list s;

    // vs_start是宏
    va_start(s, n); // s = &n + 1,让指针先后移动n自身大小个字节。正好指向可变部分的开头处地址。

    int sum = 0;
    while (n)
    {
        sum += va_arg(s, int); // 根据类型提取参数,提取完后继续让s移动自身大小个字节
        n--;
    }

    va_end(s); // 把s指针置为NULL
    return sum;
}